{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e27703e",
   "metadata": {},
   "source": [
    "# Column Value Range Metric"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "922df2f0",
   "metadata": {},
   "source": [
    "## Prepare Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c19c6681",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.datasets import fetch_openml\n",
    "\n",
    "# load \"adult\" dataset\n",
    "data = fetch_openml(name='adult', version=2, as_frame='auto')\n",
    "df = data.frame\n",
    "\n",
    "# prepare target and prediction\n",
    "df['target'] = df['education-num']\n",
    "df['prediction'] = df['education-num'].values + np.random.normal(0, 6, df.shape[0])\n",
    "\n",
    "# make reference and current datasets\n",
    "reference_data = df[~df.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "current_data = df[df.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "current_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff79d31",
   "metadata": {},
   "source": [
    "## Try to use the metric"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ade8cc77",
   "metadata": {},
   "source": [
    "Range parameters can be calculated from the reference data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5537d6ae",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from evidently.report import Report\n",
    "from evidently.metrics import ColumnValueRangeMetric\n",
    "\n",
    "report = Report(metrics=[ColumnValueRangeMetric(column_name=\"age\")])\n",
    "report.run(current_data=current_data, reference_data=reference_data)\n",
    "report"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "334db329",
   "metadata": {},
   "source": [
    "If we do not specify reference, we have to add range parameters explicitly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeede379",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "report = Report(metrics=[ColumnValueRangeMetric(column_name=\"age\", left=10, right=20)])\n",
    "report.run(current_data=current_data, reference_data=None)\n",
    "report"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169f8f0f",
   "metadata": {},
   "source": [
    "Or you can use your range with reference dataset too."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7f974ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "report = Report(metrics=[ColumnValueRangeMetric(column_name=\"age\", left=10, right=20)])\n",
    "report.run(current_data=current_data, reference_data=reference_data)\n",
    "report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6a92cd3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
